package controller;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.List;

import boundary.IHovedmenu;
import entity.DALException;
import entity.IOperatoerDTO;
import entity.WeightApp;
import functionality.IOperatoerDAO;

public class Controller implements IController {

	private IHovedmenu menu;
	private IOperatoerDAO oprFunc;
	private WeightApp app;
	
	public Controller(IHovedmenu menu, IOperatoerDAO oprFunc, WeightApp app){
		this.menu = menu;
		this.oprFunc = oprFunc;
		this.app = app;
	}
	
	@Override
	public void start() throws DALException{
		boolean running = true;
		do {
			switch (menu.start()){
			case 1:{
				if (adminCheck()){ // Tjekker om bruger ID & Password er ok
					oprAdminCon();
				} 
			}
			break;
			case 2:{
				int userID = menu.getUserID();
				String password = menu.getUserPass();
				String newPassword = menu.getUserNewPass();
				String newPassword2 = menu.getUserNewPass();
				boolean b = false;
				if (newPassword.equals(newPassword2)){
					b = oprFunc.passwordChange(userID, password, newPassword);
				}
				if (b){
					menu.commandStatus(true);
				} else {
					menu.commandStatus(false);
				}
			}
			break;
			case 3:{
				int oprID = menu.getUserID();
				IOperatoerDTO opr = oprFunc.getOperatoer(oprID);
				String input = menu.getUserPass();
				if (input.equals(opr.getPassword())){
					menu.loginStatus(true);
					try{
						java.util.Date date = new java.util.Date();						
						PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Test_App_Log.txt", true)));
						out.println("Time: "+new Timestamp(date.getTime())+" - "+"ID: "+opr.getOprId()+" - Name: "+opr.getLastName()+", "+opr.getForName());
						out.close();
					}catch (IOException e) {
						//exception handling left as an exercise for the reader
						e.printStackTrace();
					}
					app.run();					
				} else {
					menu.loginStatus(false);
				}
			}
			break;	
			case 4:{
				running=false;
				menu.showText("Programmet afsluttes.");
			}
			break;
			default: {
				menu.commandStatus(false);
			}
			}
		}
		while (running);
	}
	
	/**
	 * AdminCheck() tjekker om man har rettigheder til at foretage operatør administrationer
	 * @return True / False
	 */
	private boolean adminCheck(){
		int id = menu.getUserID();
		String pass = menu.getUserPass();

		if (id == 10 && pass.equals("02324it!")){ // Sysadmins bruger og password-check
			menu.loginStatus(true);
			return true;
		} else {
			menu.loginStatus(false);
			return false;
		}
	}
	
	@Override
	public void oprAdminCon() throws DALException{
		boolean run = true;
		do {
			int choice = menu.oprAdminMenu();
			switch (choice){
			case 1: {
				String forName = menu.oprForName();
				String lastName = menu.oprLastName();
				String cpr = menu.oprCpr();
				String oprData = oprFunc.createOperatoer(forName, lastName, cpr);
				menu.showOprCreated(oprData);
			}
			break;
			case 2:{
				menu.showText("Slet operatør");
				int oprID = menu.getUserID();
				if (oprFunc.deleteOperatoer(oprID)){
					menu.commandStatus(true);
				} else {
					menu.commandStatus(false);
				}
			}
			break;
			case 3:{
				int oprID = menu.getUserID();
				String forName = menu.oprForName();
				String lastName = menu.oprLastName();
				String newIni = oprFunc.updateOperatoer(oprID, forName, lastName);
				menu.showText("Nye initialer er : " + newIni);
			}
			break;
			case 4:{
				List<IOperatoerDTO> opr = oprFunc.getOperatoerList();
				menu.listOpr(opr);
			}
			break;
			case 5: {
				List<IOperatoerDTO> opr = oprFunc.getDelOpr();
				if(!oprFunc.getDelOpr().isEmpty()) {
					menu.listOpr(opr);
				} else {
					menu.showText("Der er ingen inaktive operatører.");
				}
			}
			break;
			case 6: {
				int oprID = menu.getUserID();
				String newPass = oprFunc.resetPassword(oprID);
				menu.showText("Password nulstillet: " + newPass);
			}
			break;
			case 7:
				run = false;
				break;
			}
		}
		while (run);
	}
}